package com.zivwu.library.ui;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zivwu.library.R;
import com.zivwu.library.dao.bean.NavBean;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.List;

 class IndexNavigatorAdapter extends CommonNavigatorAdapter {
    private List<NavBean> navBeanList;
    private OnClickListener onClickListener;


     IndexNavigatorAdapter(List<NavBean> navBeanList) {
        this.navBeanList = navBeanList;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    interface OnClickListener {
        void onClick(View view, int position);
    }


    @Override
    public int getCount() {
        return navBeanList.size();
    }

    @Override
    public IPagerTitleView getTitleView(Context context, final int i) {
        NavBean bean = navBeanList.get(i);
        CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
        commonPagerTitleView.setContentView(R.layout.ziv_nav_item);
        final ImageView titleImg = (ImageView) commonPagerTitleView.findViewById(R.id.nav_icon);
        final TextView titleText = (TextView) commonPagerTitleView.findViewById(R.id.nav_text);
        titleText.setText(bean.getTextString());
        titleImg.setImageDrawable(bean.getNorIcon());

        commonPagerTitleView.setOnPagerTitleChangeListener(
                new PageTitleChangeListener(
                        titleImg,
                        titleText));

        commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null)
                    onClickListener.onClick(v, i);
            }
        });
        return commonPagerTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        return null;
    }


    private class PageTitleChangeListener implements CommonPagerTitleView.OnPagerTitleChangeListener {
        private ImageView titleImg;
        private TextView titleText;

        private PageTitleChangeListener(ImageView titleImg, TextView titleText) {
            this.titleImg = titleImg;
            this.titleText = titleText;
        }

        @Override
        public void onSelected(int i, int i1) {
            setTitleView(navBeanList.get(i).getCheckIcon(), navBeanList.get(i).getmTextSelectedColor());
        }

        @Override
        public void onDeselected(int i, int i1) {
            setTitleView(navBeanList.get(i).getNorIcon(), navBeanList.get(i).getmTextNorColor());
        }

        @Override
        public void onLeave(int i, int i1, float v, boolean b) {

        }

        @Override
        public void onEnter(int i, int i1, float v, boolean b) {

        }


        private void setTitleView(Drawable drawable, int color) {
            titleImg.setImageDrawable(drawable);
            titleText.setTextColor(color);
        }
    }

}
